﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Threading;
using SharpDX.XAudio2;
using ScheduledXAudio2;
using SynergyControls;
using SynergyLib.DSP;

namespace XAudio2Controls
{
    public class WavePatchTabPanel : XA2TabPanelBase
    {
        #region Properties
        public Channel ChannelGetter
        {
            get { return channel; }
        }
        public SubmixVoice SubMixer
        {
            get
            {
                if (SoundEngine != null)
                {
                    return SoundEngine.SubMixer;
                }
                return null;
            }
        }
        public Thread PThread
        {
            get { return PatchThread; }
        }
        #endregion Properties

        #region Dependency Properties

        #region Channel

        public static readonly DependencyProperty ChannelProperty = DependencyProperty.Register("Channel", typeof(Channel), typeof(WavePatchTabPanel), new UIPropertyMetadata(Channel.Channel10, OnChannelChanged, OnCoerceChannel));

        private static object OnCoerceChannel(DependencyObject o, object value)
        {
            WavePatchTabPanel WavePatchTabPanel = o as WavePatchTabPanel;
            if (WavePatchTabPanel != null)
                return WavePatchTabPanel.OnCoerceChannel((Channel)value);
            else
                return value;
        }

        private static void OnChannelChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            WavePatchTabPanel WavePatchTabPanel = o as WavePatchTabPanel;
            if (WavePatchTabPanel != null)
                WavePatchTabPanel.OnChannelChanged((Channel)e.OldValue, (Channel)e.NewValue);
        }


        protected virtual Channel OnCoerceChannel(Channel value)
        {

            return value;
        }


        protected virtual void OnChannelChanged(Channel oldValue, Channel newValue)
        {

        }

        [Category("Common")]
        public Channel channel
        {
            get
            {
                return (Channel)GetValue(ChannelProperty);
            }
            set
            {
                SetValue(ChannelProperty, value);
            }
        }

        #endregion

        #endregion Dependency Properties

        #region Constructor
        static WavePatchTabPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WavePatchTabPanel), new FrameworkPropertyMetadata(typeof(XA2TabPanelBase)));
        }
        public WavePatchTabPanel()
        {            
            XA2Sequencer.TotalBarsIncreasedEvent += XA2Sequencer_TotalBarsChangedEvent;
            
        }
        void XA2Sequencer_TotalBarsChangedEvent(object sender, RoutedEventArgs e)
        {            
                AddNewRhythmButtonBar();
        }
        #endregion Constructor

        #region Public Methods
        internal override void RefreshTabPatternPanel(bool isVisible)
        {
            if (!isVisible)
            {
                TabPatternPanel.Children.Clear();
            }
            if (isVisible)
            {
                TabPatternPanel.Children.Clear();
                foreach (RhythmButtonBar rbb in Bars)
                {
                    TabPatternPanel.Children.Add(rbb);
                }
            }
        }
        internal override void TabPatternPanelRemoveBar()
        {
            if (Bars != null && Bars.Count > 0)
            {
                Bars.Remove(Bars[Bars.Count - 1]);
                RefreshTabPatternPanel(true);
            }
        }
        public override void Dispose()
        {
            IsTerminal = true;
            Engine.DisposeAHost(SoundEngine);
        }
        public override void AddNewRhythmButtonBar()
        {
            if (TabPatternPanel != null)
            {
                Bars.Add(XA2Sequencer.RegisterRhythmButtonBar(this));
            }
        }
        #endregion Public Methods

        #region Event Handlers

       

        #endregion Event Handlers
    }
}
